package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.DTLampFaultDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.SignalLampFaultDto;
import com.hualude.roadsign.model.LampFaultEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by ding on 2017/8/4.
 */
public interface SignalLampFaultRepository extends CrudRepository<LampFaultEntity, Long> {

    /**
     * @Description: 故障审核DT
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:13
     */
    public List<DTLampFaultDto> getSignalLampFaultCheck4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception;

    public Integer getSignalLampFaultCheckCount4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception;

    /**
     * @Description: 获取单位审核人审核状况
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:13
     */
    @Query(value = "select lf.lampCheck from LampFaultEntity lf where lf.lampFaultId = ?1")
    public Integer getLampCheck(Integer lampFaultId) throws Exception;

    /**
     * @Description: 主管审核通过
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:18
     */
    @Modifying
    @Query(value = "UPDATE LampFaultEntity  lf SET lf.lampMainCheck=?1, lf.lampMainCheckerId=?4,lf.lampFeedback=?2,lf.lampMaintenanceStatus=1,lf.lampMainCheckTime=?5 WHERE lf.lampFaultId=?3")
    public Integer faultAudit2Success(Integer isPass, String feedback, Integer lampFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 主管审核拒绝
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:20
     *
     */
    @Modifying
    @Query(value = "UPDATE LampFaultEntity  lf SET lf.lampMainCheck=?1, lf.lampMainCheckerId=?4,lf.lampFeedback=?2,lf.lampMainCheckTime=?5 WHERE lf.lampFaultId=?3")
    public Integer faultAudit2Fail(Integer isPass, String feedback, Integer lampFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 单位审核人审核
     * @Author: jiang-weirong
     * @Date: 2017/8/4 11:22
     */
    @Modifying
    @Query(value = "UPDATE LampFaultEntity  lf SET lf.lampCheck= ?1,lf.lampCheckerId=?4,lf.lampFeedback=?2,lf.lampCheckTime=?5 WHERE lf.lampFaultId=?3")
    public Integer faultAudit(Integer isPass, String feedback, Integer lampFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 故障处理DT
     * @Author: jiang-weirong
     * @Date: 2017/8/4 13:23
     */
    public List<DTLampFaultDto> getSignalLampFaultHandle4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer lampMaintenanceStatus) throws Exception ;

    public Integer getSignalLampFaultHandleCount4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer lampMaintenanceStatus) throws Exception;

    /**
     * @Description: 添加施工方案
     * @Author: jiang-weirong
     * @Date: 2017/8/4 13:23
     */
    @Modifying
    @Query(value = "UPDATE LampFaultEntity  lf SET lf.lampMaintenanceContent = ?2 WHERE lf.lampFaultId =?1")
    public Integer addLampMaintenanceContent(Integer lampFaultId, String lampMaintenanceContent) throws Exception;

    /**
     * @Description: 完成施工
     * @Author: jiang-weirong
     * @Date: 2017/8/4 13:24
     */
    @Modifying
    @Query(value = "UPDATE LampFaultEntity  lf SET lf.lampMaintenancePhoto = ?1,lf.lampMaintainerId=?2,lf.lampMaintenanceTime=?3,lf.lampMaintenanceExplain=?4,lf.lampMaintenanceStatus=2 WHERE lf.lampFaultId =?5")
    public Integer finishLampMaintenance(String lampMaintenancePhoto, Integer lampMaintainerId, String lampMaintenanceTime, String lampMaintenanceExplain, Integer lampFaultId) throws Exception;

    /**
     * @Description: 获取故障审核list4App
     * @Author: jiang-weirong
     * @Date: 2017/8/4 18:27
     */
    public List<SignalLampFaultDto> getLampFaultCheckList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception;

    /**
     * @Description: 信号灯详情
     * @Author: jiang-weirong
     * @Date: 2017/8/4 20:01
     */
    @Query(value = "select new com.hualude.roadsign.dto.SignalLampFaultDto(sl.signalLampId,sl.signalLampLongitude," +
            "sl.signalLampLatitude,sl.signalLampProvince,sl.signalLampCity,sl.signalLampArea,sl.signalLampStartRoad," +
            "sl.signalLampEndRoad,sl.signalLampPhoto,sl.signalLampType,sl.signalLampBrand,sl.signalLampInstallOrg," +
            "sl.signalLampCreatorId,sl.signalLampMainOrgId, sl.signalLampOrgId) " +
            "from SignalLampEntity sl where sl.signalLampId=?1")
    public SignalLampFaultDto getSignalLampInfo2Fault(Integer signalLampId) throws Exception;

    /**
     * @Description: 信号灯故障详情
     * @Author: jiang-weirong
     * @Date: 2017/8/4 20:29
     */
    @Query(value = "select new com.hualude.roadsign.dto.SignalLampFaultDto(lf.lampLevel,lf.lampPhoto,lf.lampType,lf.lampReason," +
            "lf.lampCreatorId,lf.lampTime,lf.lampCheck,lf.lampMainCheck,lf.lampFeedback, lf.lampMoney) " +
            "from LampFaultEntity lf where lf.lampFaultId=?1")
    public SignalLampFaultDto getSignalLampFaultInfo2Fault(Integer lampFaultId) throws Exception;

    /**
     * @Description: 信号灯故障datatable
     * @Author: pan-jianan
     * @Date: 2017/8/5 12:16
     */
    public Integer getLampReportFaultCount4DT(Integer actorId, Integer userId, Integer orgId, String strSearch) throws Exception;

    public List<DTLampFaultDto> getLampReportFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer userId, Integer orgId) throws Exception;

    /**
     * @Description: 更新故障
     * @Author: pan-jianan
     * @Date: 2017/8/5 12:16
     */
    @Modifying
    @Query(value = "UPDATE LampFaultEntity  lf SET lf.lampLevel = ?2,lf.lampType = ?3,lf.lampPhoto = ?4,lf.lampReason = ?5,lf.lampMoney =?6 WHERE lf.lampFaultId =?1")
    public Integer updateSignalLampFault(Integer lampFaultId, Integer lampLevel, Integer lampType, String lampPhoto, String lampReason, Double lampMoney) throws Exception;

    /**
     * @Description: 删除信号灯故障
     * @Author: pan-jianan
     * @Date: 2017/8/5 12:16
     */
    @Modifying
    @Query(value = "delete from LampFaultEntity lf where lf.lampFaultId=?1")
    public Integer deleteLampFaultByIds(Integer lampFaultId) throws Exception;

    /**
     * @Description: 删除信号灯故障
     * @Author: xu_jiaHui
     * @Date: 2017/7/26 13:05
     */
    @Modifying
    @Query(value = "delete from LampFaultEntity where signalLampId=?1")
    public Integer deleteLampFault(Integer signalMachId) throws Exception;

    /**
     * @Description: 是否可上报
     * @Author: pan-jianan
     * @Date: 2017/8/5 17:50
     */
    @Query(value = "select new com.hualude.roadsign.dto.SignalLampFaultDto(lf.lampCheck,lf.lampMainCheck,lf.lampMaintenanceStatus) " +
            "from LampFaultEntity lf where lf.signalLampId=?1  ORDER BY lf.lampTime DESC ")
    public List<SignalLampFaultDto> getLampFaultIsReport(Integer signalLampId) throws Exception;

    /**
     * @Description: 故障列表list4App
     * @Author: jiang-weirong
     * @Date: 2017/8/5 19:27
     */
    public List<SignalLampFaultDto> getLampFaultReportList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception;

    /**
     * @Description: app获取信号机故障维修列表
     * @Author: pan-jianan
     * @Date: 2017/8/5 19:27
     */
    public List<SignalLampFaultDto> getLampFaultHandleList4App(Integer orgId, Integer maintenanceStatus, String actorIds, Integer count, Integer size)throws Exception;

    /**
     * @Description: app 信号灯回执单
     * @Author: pan-jianan
     * @Date: 2017/8/5 19:27
     */
    @Query(value = "select new com.hualude.roadsign.dto.SignalLampFaultDto(sl.signalLampId,lf.lampLevel, lf.lampPhoto," +
            "lf.lampType,lf.lampReason,lf.lampMainCheckTime,lf.lampMaintenancePhoto,lf.lampMaintenanceContent,lf.lampMaintenanceExplain," +
            "lf.lampMaintenanceTime, sl.signalLampLongitude, sl.signalLampLatitude, sl.signalLampProvince, sl.signalLampCity," +
            " sl.signalLampArea, sl.signalLampStartRoad,sl.signalLampEndRoad,lf.lampFeedback)" +
            " from SignalLampEntity sl,LampFaultEntity lf " +
            "WHERE sl.signalLampId=lf.signalLampId AND lf.lampFaultId = ?1")
    public SignalLampFaultDto getLampReceiptInfo4App(Integer lampFaultId) throws Exception;

    /**
     * @Description: 获取推送相关信息
     * @Author: jiang-weirong
     * @Date: 2017/9/11 10:56
     */
    @Query(value = "select new com.hualude.roadsign.dto.SignalLampFaultDto(signalLampId, lampType, lampOrgId, lampMainOrgId) from LampFaultEntity where lampFaultId = ?1")
    public SignalLampFaultDto getInfoByFaultId(Integer lampFaultId) throws Exception;

    @Modifying
    @Query(value = "UPDATE LampFaultEntity  lf SET lf.lampCheck= ?1,lf.lampCheckerId=?4,lf.lampFeedback=?2,lf.lampCheckTime=?5,lf.lampMainCheck=?1,lf.lampMainCheckTime=?5,lf.lampMaintenanceStatus=1 WHERE lf.lampFaultId=?3")
    public Integer straightFaultAudit(Integer isPass, String feedback, Integer lampFaultId, Integer userId, String dateNowStr);
}
